package com.spring.cloud.module.common.core;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.logging.log4j.LogManager;

import java.io.Serializable;

/**
 * <p>对象基础类</p>
 * <p>所有对象都必须要继承此类</p>
 *
 * @author wb-tianzd
 * @version $Id: BaseObject.java, v 0.1 2013-3-18 下午03:15:15 wb-tianzd Exp $
 */
public class BaseObject implements Serializable {


    private static final long serialVersionUID = 5851264964557699519L;

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }


    /**
     * 格式化输出json格式的文本, 主要用于日志打印.
     * 未来增加annotation nolog 用于标识无需日志的字段名称, 避免输出的日志过于庞大
     *
     * @return
     */
    public String toLogJsonString() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        String jsonString = "";
        try {
            jsonString = mapper.writeValueAsString(this);
        } catch (JsonProcessingException e) {
            LogManager.getLogger(this).trace(e.getMessage(), e);
        }
        return jsonString;
    }

}
